package com.real8.romemp.client.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.SpanElement;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Widget;
import com.real8.romemp.client.GameMessages;
import com.real8.romemp.client.place.ShipBuildingPlace.EMenuSelection;

public class ShipBuildingViewImpl extends Composite implements ShipBuildingView {
	private static ShipBuildingViewImplUiBinder uiBinder = GWT
			.create(ShipBuildingViewImplUiBinder.class);

	interface ShipBuildingViewImplUiBinder extends
			UiBinder<Widget, ShipBuildingViewImpl> {
	}

	public ShipBuildingViewImpl() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	private Presenter presenter;

	@UiField
	HTMLPanel noMoreShipsToBuildPanel;

	@UiField
	HTMLPanel menuPanel;
	@UiField
	SpanElement shipsToBuildSpan;
	@UiField
	SpanElement marinesWithoutShipSpan;
	@UiField
	SpanElement merchantsWithoutShipSpan;

	@UiField
	QuestionAndAnswerView qaView;

	@Override
	public void setPresenter(Presenter aPresenter) {
		this.presenter = aPresenter;

		qaView.setPresenter(new QuestionAndAnswerView.Presenter() {
			@Override
			public void onAnswer(int answer) {
				presenter.onAnswer(answer);
			}
		});
	}

	@Override
	public void hideAll() {
		noMoreShipsToBuildPanel.setVisible(false);
		menuPanel.setVisible(false);
		qaView.setVisible(false);
	}

	@Override
	public void showNoMoreShipsToBuild() {
		noMoreShipsToBuildPanel.setVisible(true);
	}

	@Override
	public void showMenu(int shipsToBuild, int marinesWithoutShip,
			int merchantsWithoutShip) {
		shipsToBuildSpan.setInnerText("" + shipsToBuild);
		marinesWithoutShipSpan.setInnerText("" + marinesWithoutShip);
		merchantsWithoutShipSpan.setInnerText("" + merchantsWithoutShip);

		menuPanel.setVisible(true);
	}

	@UiHandler("warshipButton")
	void onWarshipButtonClicked(ClickEvent e) {
		if (presenter != null) {
			presenter.onSelected(EMenuSelection.WARSHIPS);
		}
	}

	@UiHandler("merchantshipButton")
	void onMerchantshipButtonClicked(ClickEvent e) {
		if (presenter != null) {
			presenter.onSelected(EMenuSelection.MERCHANTSHIPS);
		}
	}

	@UiHandler("doNothingButton")
	void onDoNothingButtonClicked(ClickEvent e) {
		if (presenter != null) {
			presenter.onSelected(EMenuSelection.DO_NOTHING);
		}
	}

	@Override
	public void showQuestion(EMenuSelection selection) {
		GameMessages messages = (GameMessages) GWT.create(GameMessages.class);

		switch (selection) {
		case WARSHIPS:
			qaView.showQuestion(messages.toBuildWarShips());
			break;
		case MERCHANTSHIPS:
			qaView.showQuestion(messages.toBuildMerchantShips());
			break;
		}
	}
}
